<template>
	<view class="Box">
		<view class="left">
			<radio @click="click_radio_all" value="" :checked="goods_totle === settle_num" /> <text>全选</text>
		</view>
		<view class="mid">
			<view class="">合计:<text>￥{{settle_price_all}}</text></view>
		</view>
		<view class="right" @click="to_settle">结算({{settle_num}})</view>
	</view>
</template>

<script>
	import {mapGetters, mapState, mapMutations} from 'vuex'
	export default {
		data() {
			return {
				
			};
		},
		computed: {
			...mapGetters('m_shopCar', ["goods_totle", "settle_num", "settle_price_all"]),
			...mapGetters('m_user', ["adress_str"]),
			...mapState('m_user', ["token"]),
			...mapState('m_shopCar', ["car_list"])
		},
		methods: {
			...mapMutations('m_user', ["set_un_login_go_login_history"]),
			...mapMutations('m_shopCar', ["del_pay_ok_goods"]),
			click_radio_all() {
				this.$emit('all_radio_change', !(this.goods_totle === this.settle_num))
			},
			to_settle() {
				const {adress_str, settle_num, token} = this
				if(!adress_str) return uni.$showMsg('请选择地址!');
				if(!settle_num) return uni.$showMsg('请选择商品!');
				if(!token) {
					let time = 5;
					uni.$showMsg('未登录,'+time+'秒后将为你跳转到登录页面', 'none', 1500)
					this.timer = setInterval(() => {
						time --;
						uni.$showMsg('未登录,'+time+'秒后将为你跳转到登录页面', 'none', 1500)
						if(time === 0) {
							clearInterval(this.timer)
							this.set_un_login_go_login_history({
								url: '../../pages/shopCar/shopCar',
								type: 'switchTab'
							});
							return uni.switchTab({
								url:'../../pages/my/my'
							})
						}
					}, 1000);
				}
				// 通过验证 开始请求支付api
				this.pay_moany();
			},
			async pay_moany() {
				// 创建订单 准备请求参数
				const req_data = {
					// order_price: this.settle_price_all,
					order_price: 0.01,
					consignee_addr: this.adress_str,
					goods: this.car_list.filter(item => item.goods_state).map((item) => {
						return {
							goods_id: item.goods_id,
							goods_number: item.goods_count,
							goods_price: item.goods_price
						}
					})
					
				}
				const res = await uni.$http.post('/api/public/v1/my/orders/create', req_data);
				// 假设成功 得到订单编号
				const order_number = 'HMDD20200901000000001949';
				
				// 发起预支付请求获取微信支付api 参数
				const res_2 = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', {order_number: order_number});
				// if(res_2.data.meta.status != 200) return uni.$showMsg('出错了');
				// 假设成功 得到支付请求参数
				const pay_req_data = {
					nonceStr:"IYKlc5iscUgZIfxj",
					package:"prepay_id=wx011604003115645ec1be4d48174ea70000",
					paySign:"AD04ED5A386B52864E176A5DF26CF436",
					signType:"MD5",
					timeStamp:"1598947444"
				}
				
				// 发起支付api
				const [err, succ] = await uni.requestPayment(pay_req_data);
				// if(err) return uni.$showMsg('出错啦')
				// 假设成功
				
				// 调用接口检测 订单是否支付成功
				const res_3 = await uni.$http.post('/api/public/v1/my/orders/chkOrder', {order_number: order_number});
				 // 假设成功
				 uni.$showMsg('支付成功', 'success')
				 
				 // 支付完成删除购物车里的 已经支付的商品
				 this.del_pay_ok_goods(this.car_list.filter(item => item.goods_state === false));
				 
				 setTimeout(() => {
					 return uni.switchTab({
					 	url:'../../pages/index/index'
					 }, 1000)
				 });
			}
		}
		
	}
</script>

<style lang="scss">
	.Box {
		z-index: 999;
		width: 100%;
		height: 80rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background-color: #ffffff;
		.left {
			margin-left: 10rpx;
		}
		.mid {
			text {
				color: #be0002;
			}
		}
		.right {
			background-color: #be0002;
			height: 100%;
			padding: 0px 50rpx;
			line-height: 80rpx;
			color: #fff;
		}
	}
</style>
